package com.wd.jetpack.demo.paging.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.paging.PagedListAdapter;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.wd.base.util.Logger;
import com.wd.common.glide.LbGlide;
import com.wd.jetpack.R;
import com.wd.jetpack.demo.paging.model.Movie;

/**
 * NA<BR>
 *
 * @author W4255
 * @version [V0.0.1, 2021/6/30]
 * @Description: NA
 */
public class MoviePagedListAdapter extends PagedListAdapter<Movie, MoviePagedListAdapter.MovieViewHolder> {

    public MoviePagedListAdapter() {
        super(DIFF_CALLBACK);
    }

    @NonNull
    @Override
    public MovieViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_demo_paging_layout, parent, false);
        return new MovieViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MovieViewHolder holder, int position) {
        Movie movie = getItem(position);
        if (movie != null) {
            LbGlide.with(holder.itemView.getContext())
                    .loading(R.mipmap.ic_launcher)
                    .load(movie.getCover())
                    .into(holder.mImage);
            holder.mTitle.setText(movie.getTitle());
            holder.mContext.setText(movie.getContent());
        }

    }

    public class MovieViewHolder extends RecyclerView.ViewHolder {
        private ImageView mImage;
        private TextView mTitle;
        private TextView mContext;

        public MovieViewHolder(@NonNull View itemView) {
            super(itemView);
            mImage = itemView.findViewById(R.id.iv_image);
            mTitle = itemView.findViewById(R.id.iv_title);
            mContext = itemView.findViewById(R.id.iv_context);
        }
    }

    private static DiffUtil.ItemCallback<Movie> DIFF_CALLBACK =
            new DiffUtil.ItemCallback<Movie>() {
                @Override
                public boolean areItemsTheSame(Movie oldConcert, Movie newConcert) {
                    return oldConcert.getTitle().equals(newConcert.getTitle());
                }

                @Override
                public boolean areContentsTheSame(Movie oldConcert,
                                                  Movie newConcert) {
                    return oldConcert.equals(newConcert);
                }
            };
}
